# read in data
library(stringr)
library(data.table)
library(ggplot2)
library(dplyr)
library(academictwitteR)
library(longurl)
library(lubridate)
library(vader)
library(lfe)
library(sjPlot)
library(stargazer)
library(standardize)
library(urltools)
library(broom)
library(fixest)

##########################################################
setwd()
##########################################################

FB_Preprocessed_Local <- readRDS("~/Data/FB_Preprocessed_Local.rds")
FBlinks_Preprocessed_Local <- readRDS("~/Data/FBlinks_Preprocessed_Local.rds")
Tweets_Preprocessed_Local <- readRDS("~/Data/Tweets_Preprocessed_Local.rds")
Tweetslinks_Preprocessed_Local <- readRDS("~/Data/Tweetslinks_Preprocessed_Local.rds")

FB_Preprocessed_Local$party <- ifelse(FB_Preprocessed_Local$party == "R", "R", "D")
FBlinks_Preprocessed_Local$party <- ifelse(FBlinks_Preprocessed_Local$party == "R", "R", "D")
Tweets_Preprocessed_Local$party <- ifelse(Tweets_Preprocessed_Local$party == "R", "R", "D")
Tweetslinks_Preprocessed_Local$party <- ifelse(Tweetslinks_Preprocessed_Local$party == "R", "R", "D")

Tweets_Preprocessed_Local <- Tweets_Preprocessed_Local %>%
  mutate(starts_with_RT = ifelse(str_starts(text, "RT "), 1, 0))
Tweetslinks_Preprocessed_Local <- Tweetslinks_Preprocessed_Local %>%
  mutate(starts_with_RT = ifelse(str_starts(text, "RT "), 1, 0))

Tweets_Preprocessed_Local <- filter(Tweets_Preprocessed_Local, starts_with_RT == 0)
Tweetslinks_Preprocessed_Local <- filter(Tweetslinks_Preprocessed_Local, starts_with_RT == 0)

##################
nominate <- read.csv("~/Data/HSall_members 2025June.csv")

c111 <- read.csv("~/Data/Members/Members by Congress 111th.csv")
c112 <- read.csv("~/Data/Members/Members by Congress 112th.csv")
c113 <- read.csv("~/Data/Members/Members by Congress 113th.csv")
c114 <- read.csv("~/Data/Members/Members by Congress 114th.csv")
c115 <- read.csv("~/Data/Members/Members by Congress 115th.csv")
c116 <- read.csv("~/Data/Members/Members by Congress 116th.csv")
c117 <- read.csv("~/Data/Members/Members by Congress 117th.csv")

######################

nominate111 <- dplyr::select(filter(nominate, congress == "111"), icpsr, chamber, nominate_dim1, nokken_poole_dim1)
nominate112 <- dplyr::select(filter(nominate, congress == "112"), icpsr, chamber, nominate_dim1, nokken_poole_dim1)
nominate113 <- dplyr::select(filter(nominate, congress == "113"), icpsr, chamber, nominate_dim1, nokken_poole_dim1)
nominate114 <- dplyr::select(filter(nominate, congress == "114"), icpsr, chamber, nominate_dim1, nokken_poole_dim1)
nominate115 <- dplyr::select(filter(nominate, congress == "115"), icpsr, chamber, nominate_dim1, nokken_poole_dim1)
nominate116 <- dplyr::select(filter(nominate, congress == "116"), icpsr, chamber, nominate_dim1, nokken_poole_dim1)
nominate117 <- dplyr::select(filter(nominate, congress == "117"), icpsr, chamber, nominate_dim1, nokken_poole_dim1)

m111 <- merge(c111, nominate111, by = "icpsr")
m112 <- merge(c112, nominate112, by = "icpsr")
m113 <- merge(c113, nominate113, by = "icpsr")
m114 <- merge(c114, nominate114, by = "icpsr")
m115 <- merge(c115, nominate115, by = "icpsr")
m116 <- merge(c116, nominate116, by = "icpsr")
m117 <- merge(c117, nominate117, by = "icpsr")

m111$DWdist <- abs(m111$nominate_dim1 - mean(m111$nominate_dim1))
m112$DWdist <- abs(m112$nominate_dim1 - mean(m112$nominate_dim1))
m113$DWdist <- abs(m113$nominate_dim1 - mean(m113$nominate_dim1))
m114$DWdist <- abs(m114$nominate_dim1 - mean(m114$nominate_dim1))
m115$DWdist <- abs(m115$nominate_dim1 - mean(m115$nominate_dim1))
m116$DWdist <- abs(m116$nominate_dim1 - mean(m116$nominate_dim1))
m117$DWdist <- abs(m117$nominate_dim1 - mean(m117$nominate_dim1))

m111$NPdist <- abs(m111$nokken_poole_dim1 - mean(m111$nokken_poole_dim1))
m112$NPdist <- abs(m112$nokken_poole_dim1 - mean(m112$nokken_poole_dim1))
m113$NPdist <- abs(m113$nokken_poole_dim1 - mean(m113$nokken_poole_dim1))
m114$NPdist <- abs(m114$nokken_poole_dim1 - mean(m114$nokken_poole_dim1))
m115$NPdist <- abs(m115$nokken_poole_dim1 - mean(m115$nokken_poole_dim1))
m116$NPdist <- abs(m116$nokken_poole_dim1 - mean(m116$nokken_poole_dim1))
m117$NPdist <- abs(m117$nokken_poole_dim1 - mean(m117$nokken_poole_dim1))

combined <- rbind(m111, m112, m113, m114, m115, m116, m117)

combined$icpsrcongress <- paste0(as.character(combined$icpsr),"-", as.character(combined$congress))

######################

congresstweets <- Tweets_Preprocessed_Local %>%
  group_by(icpsr, congress) %>%
  dplyr::summarize(
    tweets = n(),
    local = mean(local, na.rm = TRUE)*100
  ) %>%
  ungroup()

congresstweets$icpsrcongress <-  paste0(as.character(congresstweets$icpsr),"-", as.character(congresstweets$congress))

congresstweets <- merge(congresstweets, combined, by = "icpsrcongress")

congresslinks <- Tweetslinks_Preprocessed_Local %>%
  group_by(icpsr, congress) %>%
  dplyr::summarize(
    tweets = n(),
    local = mean(Locallink, na.rm = TRUE)*100
  ) %>%
  ungroup()

congresslinks$icpsrcongress <-  paste0(as.character(congresslinks$icpsr),"-", as.character(congresslinks$congress))

congresslinks <- merge(congresslinks, combined, by = "icpsrcongress")

##

congressfb <- FB_Preprocessed_Local %>%
  group_by(icpsr, congress) %>%
  dplyr::summarize(
    tweets = n(),
    local = mean(Local, na.rm = TRUE)*100
  ) %>%
  ungroup()

congressfb$icpsrcongress <-  paste0(as.character(congressfb$icpsr),"-", as.character(congressfb$congress))

congressfb <- merge(congressfb, combined, by = "icpsrcongress")

congressfblinks <- FBlinks_Preprocessed_Local %>%
  group_by(icpsr, congress) %>%
  dplyr::summarize(
    tweets = n(),
    local = mean(Locallink, na.rm = TRUE)*100
  ) %>%
  ungroup()

congressfblinks$icpsrcongress <-  paste0(as.character(congressfblinks$icpsr),"-", as.character(congressfblinks$congress))

congressfblinks <- merge(congressfblinks, combined, by = "icpsrcongress")

##############

congresstweets<- filter(congresstweets, tweets > 4)
congresslinks<- filter(congresslinks, tweets > 4)
congressfb<- filter(congressfb, tweets > 4)
congressfblinks<- filter(congressfblinks, tweets > 4)

congresstweets$republican <- ifelse(congresstweets$party == "R",1,0)
congresslinks$republican <- ifelse(congresslinks$party == "R",1,0)
congressfb$republican <- ifelse(congressfb$party == "R",1,0)
congressfblinks$republican <- ifelse(congressfblinks$party == "R",1,0)

congresstweets$elect_year <- ifelse(is.na(congresstweets$Vote_Pct_2PT), 0,1)
congresstweets$elect_year <- ifelse(congresstweets$chamber.y == "House", 1, congresstweets$elect_year)
congresslinks$elect_year <- ifelse(is.na(congresslinks$Vote_Pct_2PT), 0,1)
congresslinks$elect_year <- ifelse(congresslinks$chamber.y == "House", 1, congresslinks$elect_year)
congressfb$elect_year <- ifelse(is.na(congressfb$Vote_Pct_2PT), 0,1)
congressfb$elect_year <- ifelse(congressfb$chamber.y == "House", 1, congressfb$elect_year)
congressfblinks$elect_year <- ifelse(is.na(congressfblinks$Vote_Pct_2PT), 0,1)
congressfblinks$elect_year <- ifelse(congressfblinks$chamber.y == "House", 1, congressfblinks$elect_year)

###########

################
library(standardize)
congresstweets$NPDistScale <- scale(congresstweets$NPdist)[, 1]
congresslinks$NPDistScale <- scale(congresslinks$NPdist)[, 1]
congressfb$NPDistScale <- scale(congressfb$NPdist)[, 1]
congressfblinks$NPDistScale <- scale(congressfblinks$NPdist)[, 1]

congresstweets$Seniority <- scale(congresstweets$Seniority)[, 1]
congresslinks$Seniority <- scale(congresslinks$Seniority)[, 1]
congressfb$Seniority <- scale(congressfb$Seniority)[, 1]
congressfblinks$Seniority <- scale(congressfblinks$Seniority)[, 1]

congresstweets$PVIAbsscale <- scale(congresstweets$PVIABS)[, 1]
congresslinks$PVIAbsscale <- scale(congresslinks$PVIABS)[, 1]
congressfb$PVIAbsscale <- scale(congressfb$PVIABS)[, 1]
congressfblinks$PVIAbsscale <- scale(congressfblinks$PVIABS)[, 1]

congresstweets$PVIPartyscale <- scale(congresstweets$PVIParty)[, 1]
congresslinks$PVIPartyscale <- scale(congresslinks$PVIParty)[, 1]
congressfb$PVIPartyscale <- scale(congressfb$PVIParty)[, 1]
congressfblinks$PVIPartyscale <- scale(congressfblinks$PVIParty)[, 1]

############
npmodellinks1 <- felm(local ~ Pres.Party + Chamber.Majority + NPDistScale + PVIAbsscale + chamber.y + female + republican + Freshman + Seniority + Cmte_Chair + elect_year| congress.x, data = congresstweets)
npmodellinks2 <- felm(local ~ Pres.Party + Chamber.Majority + NPDistScale + PVIAbsscale + chamber.y + female + republican + Freshman + Seniority + Cmte_Chair + elect_year| congress.x, data = congresslinks)
npmodellinks3 <- felm(local ~ Pres.Party + Chamber.Majority + NPDistScale + PVIAbsscale + chamber.y + female + republican + Freshman + Seniority + Cmte_Chair + elect_year| congress.x, data = congressfb)
npmodellinks4 <- felm(local ~ Pres.Party + Chamber.Majority + NPDistScale + PVIAbsscale + chamber.y + female + republican + Freshman + Seniority + Cmte_Chair + elect_year| congress.x, data = congressfblinks)

tab_model(npmodellinks1, npmodellinks2, npmodellinks3, npmodellinks4)
stargazer(npmodellinks2, npmodellinks4, npmodellinks1, npmodellinks3, type = "html", out = "Local Regression noRT.htm")

##########

npmodellinks1fe <- felm(local ~ Pres.Party + Chamber.Majority + NPDistScale + PVIAbsscale + Freshman + Cmte_Chair + elect_year| congress.x + icpsr.x, data = congresstweets)
npmodellinks2fe <- felm(local ~ Pres.Party + Chamber.Majority + NPDistScale + PVIAbsscale + Freshman + Cmte_Chair + elect_year| congress.x + icpsr.x, data = congresslinks)
npmodellinks3fe <- felm(local ~ Pres.Party + Chamber.Majority + NPDistScale + PVIAbsscale + Freshman + Cmte_Chair + elect_year| congress.x + icpsr.x, data = congressfb)
npmodellinks4fe <- felm(local ~ Pres.Party + Chamber.Majority + NPDistScale + PVIAbsscale + Freshman + Cmte_Chair + elect_year| congress.x + icpsr.x, data = congressfblinks)

tab_model(npmodellinks1fe, npmodellinks2fe, npmodellinks3fe, npmodellinks4fe)
stargazer( npmodellinks2fe, npmodellinks4fe, npmodellinks1fe, npmodellinks3fe, type = "html", out = "Local Regression FE noRT.htm")

